#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;

//将tid转乘16进制
static void printTid(const char *name,const pthread_t& tid)
{
    printf("%s 正在运行 ,tid: 0x%x\n",name,tid);

}
void *startRoutine(void *args)
{
    const char *name = static_cast<const char *>(args);
    int cnt = 5;
    while(true)
    {
        printTid(name,pthread_self());
        //cout<<"线程正在运行......"<<endl;
        sleep(1);
        // if(!(cnt--))  
        // {
        //     break;
        //     // int *p = nullptr;
        //     // *p = 100;//野指针问题
        // }
    }
    cout<<"线程退出啦........"<<endl;
    //return (void*)111;
    //pthread_exit((void*)2222);
}


int main()
{
    pthread_t tid;
    int n = pthread_create(&tid,nullptr,startRoutine,(void*)"thread 1");
    (void)n;

    sleep(3);//代表main thread对应的工作
    pthread_cancel(tid);
    cout<<"new thread been canceled"<<endl;

    void *ret = nullptr;// void* -> 字节
    pthread_join(tid,&ret);//void **retval是一个输出型参数
    cout<<"main thread join sucess , *ret:" <<(long long)ret<<endl; 

    PTHREAD_CANCELED;
    sleep(10);

    // //创建tid
    // pthread_t tid;
    // //创建线程
    // int n = pthread_create(&tid,nullptr,startRoutine,(void*)"thread 1");
    // //printTid(tid);
    // //cout<<"tid :" <<tid<<endl;
    //主线程
    while(true)
    {
        printTid("man thread:",pthread_self());
        //cout<<"我是主线程，我正在运行......"<<endl;
        sleep(1);
    }

    return 0;
}